package com.time.workbench.efficient.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.time.workbench.efficient.entity.bo.Environment;
import com.time.workbench.efficient.entity.bo.Server;
import com.time.workbench.efficient.entity.dto.EnvironmentPageReq;
import com.time.workbench.efficient.entity.dto.ServerPageReq;
import com.time.workbench.efficient.enums.EnvironmentCodeEnums;
import com.time.workbench.efficient.mapper.EnvironmentMapper;
import com.time.workbench.efficient.mapper.ServerMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 环境管理service
 */
@Service
@RequiredArgsConstructor
public class EnvironmentService extends ServiceImpl<EnvironmentMapper, Environment> implements IService<Environment> {
    private final ServerService serverService;

    public Page<Environment> page(EnvironmentPageReq environmentPageReq) {
        IPage<Environment> page = new Page<>(environmentPageReq.getCurrent(), environmentPageReq.getPageSize());
        return baseMapper.page(page, environmentPageReq);
    }

    /**
     * 根据环境code获取服务器
     *
     * @param environmentCodeEnums
     */
    public List<Server> getServerByEnvironmentCode(EnvironmentCodeEnums environmentCodeEnums) {
        Environment environment = getEnvironmentByCode(environmentCodeEnums);
        List<Server> servers = serverService.getByEnvironment(environment.getId());
        return servers;
    }

    public Environment getEnvironmentByCode(EnvironmentCodeEnums environmentCodeEnums) {
        return lambdaQuery().eq(Environment::getCode, environmentCodeEnums.name()).last("limit 1").one();
    }

}
